﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Windows.Controls;
using System.Windows;

namespace Eniux.Windows.Interactivity.Behaviors
{
    /// <summary>
    /// TextBox Text大小写转换操作
    /// </summary>
  public  class UpperOrLowerAction : TargetedTriggerAction<TextBox>
    {
        #region Action

        /// <summary>
        /// 要执行的操作类型
        /// </summary>
        public TextActions Action
        {
            get { return (TextActions)GetValue(ActionProperty); }
            set { SetValue(ActionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Action.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ActionProperty =
            DependencyProperty.Register("Action", typeof(TextActions), typeof(UpperOrLowerAction), new UIPropertyMetadata(TextActions.None, new PropertyChangedCallback(ActionPropertyChanged)));
        private static void ActionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            UpperOrLowerAction textbox = (UpperOrLowerAction)d;
            textbox.ExecuteAction();
        }
        
        #endregion 

        /// <summary>
        /// 重写方法
        /// </summary>
        /// <param name="parameter"></param>
        protected override void Invoke(object parameter)
        {
            ExecuteAction();
        }

        private void ExecuteAction()
        {
            if(Target!=null)
                switch (Action)
                {
                    case TextActions.None:
                        break;
                    case TextActions.Lower:
                        Target.Text = Target.Text.ToLower();
                        break;
                    case TextActions.Upper:
                        Target.Text = Target.Text.ToUpper();
                        break;
                    default:
                        break;
                }
        }   
    }

  /// <summary>
  /// Text可执行的操作类型
  /// </summary>
  public enum TextActions
  {
      /// <summary>
      /// 不执行操作
      /// </summary>
      None,
      /// <summary>
      /// 执行转换为小写操作
      /// </summary>
      Lower,
      /// <summary>
      /// 执行转化为大写操作
      /// </summary>
      Upper
  }
}
